package com.ksd.modules.rbac.menus.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.modules.rbac.menus.entity.SysMenus;
import com.ksd.modules.rbac.menus.po.SysMenusSavePO;
import com.ksd.modules.rbac.menus.po.SysMenusSearchPO;
import com.ksd.modules.rbac.menus.service.ISysMenusService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
public class SysMenusController  extends BaseController {

    @Autowired
    private ISysMenusService sysMenusService;

    /**
     * 查询首页有系统日志接口
     *
     * @return
     */
    @PostMapping("/sysmenus/root")
    public R<List<SysMenus>> findMenuRoot() {
        return R.data(sysMenusService.findMenuRoot());
    }
    /**
     * 查询首页有系统日志接口
     *
     * @return
     */
    @PostMapping("/sysmenus/list")
    public R<List<SysMenus>> findSysMenus(@Validated @RequestBody SysMenusSearchPO sysMenusSearchPO) {
        return R.data(sysMenusService.findMenu(sysMenusSearchPO));
    }

    /**
     * 保存和更新
     *
     * @param adminSysMenusSavePO
     * @return
     */
    @PostMapping("/sysmenus/saveupdate")
    @ApiLog("菜单保存和更新")
    public R<Boolean> saveUpdateSysMenus(@Validated @RequestBody SysMenusSavePO adminSysMenusSavePO) {
        return R.data(sysMenusService.saveUpdateSysMenus(adminSysMenusSavePO));
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/sysmenus/get/{id}")
    public R<SysMenus> getSysMenusById(@PathVariable("id") Long id) {
        return R.data(sysMenusService.getSysMenusById(id));
    }

    /**
     * 根据id删除菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/sysmenus/del/{id}")
    @ApiLog("菜单保存和更新")
    public R<Boolean> delSysMenusById(@PathVariable("id") Long id) {
        return R.data(sysMenusService.deleteSysMenusById(id));
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/sysmenus/status/{id}/{field}/{value}")
    @ApiLog("菜单修改状态")
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(sysMenusService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(sysMenusService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }
}
